package com.yuyang.wifi.utils;

import android.text.InputFilter;
import android.text.Spanned;
import android.widget.EditText;

import com.yuyang.wifi.MyApplication;
import com.yuyang.wifi.model.InputModel;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InputEmojiFilterUtils {

    public static void setEditextView(List<InputModel> etListModel) {

        if (null != etListModel && etListModel.size() != 0) {
            for (int i = 0; i < etListModel.size(); i++) {
                EditText editText = etListModel.get(i).getEditText();
                int length = etListModel.get(i).getLength();
                if (length == 0) {
                    editText.setFilters(new InputFilter[]{InputEmojiFilterUtils.getInputFilter()});
                } else {
                    editText.setFilters(new InputFilter[]{InputEmojiFilterUtils.getInputFilter(), new InputFilter.LengthFilter(length)});
                }
            }
        }
    }


    public static InputFilter getInputFilter() {

        InputFilter inputFilter = new InputFilter() {

            Pattern pattern = Pattern.compile("[^a-zA-Z0-9\\u4E00-\\u9FA5_,.?!:;…~_\\-\"\"/@*+'()<>{}/[/]()<>{}\\[\\]=%&$|\\/♀♂#¥£¢€\"^` ，•。？！\\n：；……～“”、“（）”、（——）‘’＠‘·’＆＊＃/《》￥《〈〉》£〈＄〉［］￡［］｛｝｛｝￠【】【】％〖〗〖〗／〔〕€ £Ұ₴$₰¢₤¥₳₲₪₵元₣₱฿¤₡₮₭₩ރ円₢₥₫₦zł﷼₠₧₯₨Kčर₹ƒ₸￠〔〕＼『』『』＾「」「」｜﹁﹂｀．]");

            @Override
            public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
                Matcher matcher = pattern.matcher(charSequence);
                if (!matcher.find()) {
                    String content = charSequence.toString();
                    if (!content.equals(" ")) {
                        if (content.equals("\n")) {
                            return "";
                        } else {
                            return null;
                        }
                    }
                    return "";
                    //禁止换行
                    // return charSequence.toString().replace("\n", null);
                    //return null;
                } else {
                    ToastUtils.showShortToast(MyApplication.getInstance(), "不支持字符！");
                    return "";
                }
            }
        };
        return inputFilter;
    }

}
